var addUser = function () {
    
    return {
        //main function to initiate the module
        init: function () {
        	var form2 = $('.addUser-form');
			var error2 = $('.alert-error', form2);
        	var success2 = $('.alert-success', form2);
			
			var validator = form2.validate({
				errorElement: 'span', //default input error message container
                errorClass: 'help-inline', // default input error message class
                focusInvalid: false, // do not focus the last invalid input
                ignore: "",
                rules: {
                    username: {
                        minlength: 8,
                        required: true,
                        remote: {
                        	type: "POST",
                        	url: $('#APP_ROOT_PATH').val()+"user/validate_account?id=" + $("#id").val(),
                        	data:{
                        		account: function(){
                        			return $("#username").val();
                        		}	
                        	}
                        }
                    },
                    name: {
                        required: true
                    },
                    roleId:{
                    	required: true
                    }
                },
                messages: {
                	username:{remote:jQuery.format("用户名已经被注册")},
                },
				
                invalidHandler: function (event, validator) { //display error alert on form submit   
                    success2.hide();
                    error2.show();
                },

                highlight: function (element) { // hightlight error inputs
                    $(element)
                        .closest('.help-inline').removeClass('ok'); // display OK icon
                    $(element)
                        .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
                },

                unhighlight: function (element) { // revert the change dony by hightlight
                    $(element)
                        .closest('.control-group').removeClass('error'); // set error class to the control group
                },

                success: function (label) {
                    if (label.attr("for") == "service" || label.attr("for") == "membership") { // for checkboxes and radip buttons, no need to show OK icon
                        label
                            .closest('.control-group').removeClass('error').addClass('success');
                        label.remove(); // remove error label here
                    } else { // display success icon for other inputs
                        label
                            .addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
                        .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group
                    }
                },
	
				submitHandler:function(form){
                    addUser.subForm();
                },
			
			})
    	},
    	restForm:function(){
			$(".addUser-form div").removeClass('error');
			$(".addUser-form div").removeClass('success');
			$(".addUser-form div div span").empty();
			validator.resetForm(); 
    	},
    	subForm:function(){
    		var type = $("#type").val();
    		var account = $("#username").val();
    		var name = $("#name").val();
    		var remark = $("#remark").val();
    		var roleId = $("#roleId").val();
    		var id = $("#id").val();
    		
			gui.util.Ajax.asyncPOST($('#APP_ROOT_PATH').val()+"user/addUser_done", {'id':id,'roleId':roleId,'type':type, 'account': account, 'name': name, 'remark': remark }, function(result) {
				if(result=="y"){
					location.href=$('#APP_ROOT_PATH').val()+"user/index";
				}else{
					$('.alert-error', $('.addUser-form')).find("span").html("添加失败");
					$('.alert-error', $('.addUser-form')).show();
				}
			})
			
    	}
    }

}();